<?php
namespace app\system\controller;

use app\common\model\ConfigGroupModel;
use think\Request;

class ConfigGroupController extends BaseController
{

    private $configGroupModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->configGroupModel = new ConfigGroupModel();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $list = $this->configGroupModel->order('group_sort asc')->select();
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加用户
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'ConfigGroupValidate');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->configGroupModel->addData($data)) {
                $this->success('添加成功！', url('index'));
            } else {
                $this->error($this->configGroupModel->getError());
            }
        }
        return $this->fetch();
    }

    /**
     * 修改数据
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'ConfigGroupValidate');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->configGroupModel->editData($data)) {
                $this->success('修改成功！', url('index'));
            } else {
                $this->error($this->configGroupModel->getError());
            }
        }
        $id   = $this->request->param('id');
        $info = $this->configGroupModel->get($id);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除数据
     */
    public function del()
    {
        $id = $this->request->param('id', 0);
        if ($this->configGroupModel->delData($id)) {
            $this->success('删除成功！');
        } else {
            $this->error($this->configGroupModel->getError());
        }
    }

}
